package org.superheroes.gui;

import org.superheroes.discovery.R;
import org.superheroes.net.StreamingServer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * The main activity class
 * 
 * @author Jeffrey Goudzwaard
 * @author Davin Terrizzi
 * @version 1.0
 */
public class MainActivity extends Activity
{
	private final String LOG_TAG = MainActivity.class.getName();
	private final String LOG_ERROR_TAG = MainActivity.class.getName() + ".error";
	
	private Button startButton;
	private TextView txtStatus;
	private String status;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_main );
		
		initControls();
		
		runOnUiThread( new Runnable()
		{
			
			@Override
			public void run()
			{
				if( status != null )
				{
					txtStatus.setText( status );
					Log.i( LOG_TAG, "Update status to: " + status );
				}
			}
		} );
	}
	
	/**
	 * Initializes the controls
	 */
	private void initControls()
	{
		status = "Init controls";
		
		Log.i( LOG_TAG, status );
		
		txtStatus = ( TextView ) findViewById( R.id.textView1 );
		
		startButton = ( Button ) findViewById( R.id.button1 );
		startButton.setOnClickListener( new OnClickListener()
		{
			
			@Override
			public void onClick( View v )
			{
				status = "Start button clicked!";
				Log.i( LOG_TAG, status );
				
				
				Thread t = new Thread( new Runnable()
				{
					
					@Override
					public void run()
					{
						try
						{
							init();
						}
						catch( Exception e )
						{
							Log.e( LOG_ERROR_TAG, e.toString() );
						}
					}
				} );
				
				t.start();
			}
		} );
	}
	
	/**
	 * Initializes the server
	 * 
	 * @throws Exception
	 */
	private void init() throws Exception
	{
		status = "Init server";
		Log.i( LOG_TAG, status );
		
		new StreamingServer();
	}

	@Override
	public boolean onCreateOptionsMenu( Menu menu )
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate( R.menu.main, menu );
		return true;
	}

}
